# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import _thread as thread
import json

import tushare as ts
from django.http import HttpResponse
from django.shortcuts import render
from djangoProject import settings

import gplucky.classifying as cl
import gplucky.cron as cn
import gplucky.trading as td

api = ts.pro_api(settings.TUSHARE_TOKEN)

def test(request):
    cn.cron.test(request)
    return render(request, "gplucky/index.html")


def index(request):
    return render(request, "gplucky/index.html")


# 获取实时数据
def getInfo(request):
    code = request.GET.get("code")
    # pageHtml = mark_safe(cn.getCurrentInfoSingle(code))
    pageHtml = cn.cron.getCurrentInfoSingle(request, code)
    return render(request, "gplucky/info.html", {'info': pageHtml})


# 获取实时数据(json返回)
def getInfoJson(request):
    code = request.GET.get("code")
    return HttpResponse(cn.cron.getInfoJson(request, code), content_type="application/json")


def syncTodayInfo(request):
    try:
        thread.start_new_thread(cn.cron.syncTodayInfo(request), (1,))
    except Exception as e:
        print(e)
    return render(request, "gplucky/index.html")

def getTodayAll(request):
    # return HttpResponse(json.dumps(cn.getTodayAll()), content_type="application/json")
    return HttpResponse(ts.get_today_all(), content_type="application/json")


def syncHistory(request):
    start = request.POST.get("start")
    end = request.POST.get("end")
    try:
        thread.start_new_thread(cn.cron.syncHistory(request, start, end), (start, end,))
    except Exception as e:
        print(e)
    return render(request, "gplucky/index.html")

# 获取资金实力最强的营业部当天买入的股票集合
def getZjslYyb(request):
    day = request.POST.get("day")
    return HttpResponse(json.dumps(cn.cron.getZjslYyb(request, day)), content_type="application/json")


# 获取实时分笔
def get_realtime_quotes(request):
    codes=""
    if request.method == 'GET':
        codes = request.GET.get("codes")
    elif request.method == 'POST':
        codes = request.POST.get("codes")
    df = ts.get_realtime_quotes(codes.split(','))
    return HttpResponse(df.to_json(orient='records'), content_type="application/json")


# 获取每日收盘行情
def get_day_all(request):
    day = ""
    try:
        if request.method == 'GET':
            day = request.GET.get("day")
        elif request.method == 'POST':
            day = request.POST.get("day")
        cn.cron.get_day_all(request, day)
    except Exception as e:
        print(e)
    return render(request, "gplucky/index.html")

def get_today_ticks(request):
    code=""
    if request.method == 'GET':
        code = request.GET.get("code")
    elif request.method == 'POST':
        code = request.POST.get("code")
    return HttpResponse(td.trading.get_today_ticks(request, code), content_type="application/json")


def get_today_ticks_proxy(request):
    code=""
    proxy=""
    if request.method == 'GET':
        code = request.GET.get("code")
        proxy = request.GET.get("proxy")
    elif request.method == 'POST':
        code = request.POST.get("code")
        proxy = request.POST.get("proxy")
    return HttpResponse(td.trading.get_today_ticks_proxy(request, code, proxy), content_type="application/json")


# 大单交易数据
def get_sina_dd(request):
    code=""
    date=""
    volume=""
    if request.method == 'GET':
        code = request.GET.get("code")
        date = request.GET.get("date")
        volume = request.GET.get("vol")
    elif request.method == 'POST':
        code = request.POST.get("code")
        date = request.POST.get("date")
        volume = request.POST.get("vol")
    return HttpResponse(td.trading.get_sina_dd(request, code, date, volume), content_type="application/json")

# 一次性获取最近一个日交易日所有股票的交易数据
def get_today_all(request):
    return HttpResponse(ts.get_today_all().to_json(orient='records'), content_type="application/json")


# 初始化股票分类（只初始化行业、概念、地域）
def init_classifying(request):
    date=""
    try:
        if request.method == 'GET':
            date = request.GET.get("date")
        elif request.method == 'POST':
            date = request.POST.get("date")
        initClassifying(date)
    except Exception as e:
        print("Error: %s", e)
    return render(request, "gplucky/index.html")


def initClassifying(date):
    cl.init_classifying(date)


def new_stocks(request):
    try:
        thread.start_new_thread(cn.cron.new_stocks(request), (1,))
    except Exception as e:
        print(e)
    return render(request, "gplucky/index.html")

# 获取大盘指数行情
def get_index(request):
    return HttpResponse(ts.get_index().to_json(orient='records'), content_type="application/json")

#获取每日行情，包含均线
def get_daily_ma(request):
    trade_date = ""
    if request.method == 'GET':
        trade_date = request.GET.get("trade_date")
    elif request.method == 'POST':
        trade_date = request.POST.get("trade_date")
    cn.cron.pro_bar_daily_ma(trade_date)
    return render(request, "gplucky/index.html")

# 获取历史分笔
def get_tick_data(request):
    code = ""
    date = ""
    if request.method == 'GET':
        date = request.GET.get("date")
        code = request.GET.get("code")
    elif request.method == 'POST':
        date = request.POST.get("date")
        code = request.POST.get("code")
    return HttpResponse(ts.get_tick_data(code,date=date,src='tt').to_json(orient='records'), content_type="application/json")

# 获取历史分笔（根据天来，并存储到数据库中）
def init_tick_data_by_date(request):
    date = ""
    isCallback = ""
    if request.method == 'GET':
        date = request.GET.get("date")
        isCallback = request.GET.get("isCallback")
    elif request.method == 'POST':
        date = request.POST.get("date")
        isCallback = request.POST.get("isCallback")
    td.trading.init_tick_data_by_date(date, isCallback)
    return render(request, "gplucky/index.html")

# 获取历史分笔（code、date,并村塾到数据库中）
def sync_tick_data_by_code_date(request):
    code = ""
    date = ""
    if request.method == 'GET':
        date = request.GET.get("date")
        code = request.GET.get("code")
    elif request.method == 'POST':
        date = request.POST.get("date")
        code = request.POST.get("code")
    td.trading.sync_tick_data_by_code_date(date, code)
    return render(request, "gplucky/index.html")